<?php

class AmbientesController extends Site_Controller_Action
{
    
    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $id = $this->_getParam('id', 0);
        
        $query = 'SELECT SQL_CACHE * FROM AMBIENTES WHERE AMB_STA = 1 ORDER BY AMB_ORD ASC LIMIT 1';
        $result = $this->db->fetchRow($query);
        $this->view->ambiente = $result;
        
    }
    
    public function detalheAction()
    {
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/jquery-ui-1.10.2.custom/css/custom-theme/jquery-ui-1.10.2.custom.css'); 
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery-ui-1.10.2.custom/js/jquery-1.9.1.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery-ui-1.10.2.custom/js/jquery-ui-1.10.2.custom.min.js');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/validation/dist/jquery.validate.min.js');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskMoney.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/jquery.maskedinput.min.js');
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/mask.js');
        
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/fancybox/source/jquery.fancybox.pack.js?v=2.1.5');
        $this->view->headLink()->prependStylesheet($this->_urlbase.'/public/media/js/fancybox/source/jquery.fancybox.css?v=2.1.5'); 
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/ambientes.js?1='.rand(99,9999));
                
        $id = $this->_getParam('id', 0);
        
        $query = 'SELECT SQL_CACHE * FROM AMBIENTES WHERE AMB_ID = '.$id;
        $result = $this->db->fetchRow($query);
        $this->view->ambiente = $result;
        
        $query = 'SELECT SQL_CACHE a.COR_ID FROM AMBIENTES_CORES a, CORES c WHERE a.COR_ID = c.COR_ID AND a.AMB_ID = '.$id;
        $result = $this->db->fetchAll($query);
        $this->view->cores = $result;
        
        
        $query = 'SELECT SQL_CACHE a.ACA_ID FROM AMBIENTES_ACABAMENTOS a, ACABAMENTOS c WHERE a.ACA_ID = c.ACA_ID AND a.AMB_ID = '.$id;
        $result = $this->db->fetchAll($query);
        $this->view->acabamentos = $result;
        
    }
    
    
    public function orcamentoAction() {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $msg = 'Sistema indisponível. Tente novamente mais tarde!';
        
        if ($this->getRequest()->isPost()) {
            try {
                $formData = $this->getRequest()->getPost();
                
                $data = array(
                    'INT_NOM' => $formData['nome'],
                    'INT_TEL' => $formData['telefone'],
                    'INT_EML' => $formData['email'],
                    'INT_MSG' => $formData['mensagem'],
                    'INT_DAT' => date('Y-m-d'),
                    'COR_ID'  => $formData['cores'],
                    'ACA_ID'  => $formData['acabamentos'],
                    'INT_STA' => 1,
                );

                $int = $this->db->insert('INTERESSE', $data);
                
                Zend_Loader::loadClass('envioemail','library');

                $arr['nome'] = $formData['nome'];
                $arr['email'] = $formData['email'];
                $arr['telefone'] = $formData['telefone'];
                $arr['mensagem'] = $formData['mensagem'];
                $arr['urlbase']  = $this->_urlbase;

                $ok = envioemail::Orcamento($arr);

                if($ok){
                    $msg = "Mensagem enviada com sucesso!";
                }
                
            } catch (Exception $exc) {
                //echo 'error: '.$exc->getMessage();
            }
        }
        
        echo json_encode(array('ok' => $ok, 'msg'=> $msg));
        //$this->_helper->redirector('index');
    }
    
}

